package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 历史订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<OrderVO> querryOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    List<OrderVO> select(Integer status);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    OrderVO selectOrderDetail(Long id);

    /**
     * 管理端订单搜索
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 商家接单
     * @param ordersConfirmDTO
     */
    @Update("update orders set status = 3 where  id = #{id}")
    void orderconfirm(OrdersConfirmDTO ordersConfirmDTO);

    /**
     * 商家拒单
     * @param ordersRejectionDTO
     */

    @Update("update orders set status = 6 ,  rejection_reason = #{rejectionReason} where id = #{id}")
    void orderRejection(OrdersRejectionDTO ordersRejectionDTO);

    /**
     * 订单派送
     * @param id
     */
    @Update("update orders set status = 4 where id = #{id}")
    void orderdelivery(Integer id);

    /**
     * 订单完成
     * @param id
     */
    @Update("update orders set status = 5 where id = #{id}")
    void ordercomplete(Integer id);
    @Update("update orders set status = 6 ,  cancel_reason = #{cancelReason} where id = #{id}")
    void ordercancel(OrdersCancelDTO ordersCancelDTO);

    /**
     * 获取各个订单状态数量
     * @param status
     * @return
     */
    @Select("select * from orders where status = #{status}")
    List<Orders> getByStatus(Integer status);

    /**
     * 根据订单状态和时间查询订单
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime orderTime);
    @Select("select * from orders where id = #{id}")
    Orders getById(Integer id);

    /**
     * 指定时间范围内营业额统计
     * @param map
     * @return
     */
    Double getByMap(Map map);
}
